<?php

class Default_Model_DbTable_Product extends Zend_Db_Table_Abstract
{

    protected $_name = 'products';

    public function getAllProduct(){
    	return $this->fetchAll();
    }
    
    public function getProductByID($proId){
    	return $this->fetchRow("product_id = $proId");
    }
    
    public function getProductBySale($saleID){
    	$select = $this->select()
    	->from($this->_name)
    	->where("sale_id = $saleID");
    	return $this->fetchAll($select);
    }
    
    public function getProductbyBrand($brandID){
    	$select = $this->select()
    	->from($this->_name)
    	->where("brand_id = $brandID");
    	return $this->fetchAll($select);
    }
    
    public function addProduct($proName,$catID,$desc,$sale_id,$brand_id){
    	$productData = array(
    			"product_name"=>$proName,
    			"cat_id"=>$catID,
    			"description"=>$desc,
    			"sale_id"=>$sale_id,
    			"brand_id"=>$brand_id,
    			"status"=>0
    	);
    	$this->insert($productData);
    	$lastID = $this->_db->lastInsertId($this->_name);
    	return $lastID;
    }
    
    public function updateProduct($proID,$proName,$catID,$desc,$sale_id,$brand_id,$stt){
    	$productData = array(
    			"product_name"=>$proName,
    			"cat_id"=>$catID,
    			"description"=>$desc,
    			"sale_id"=>$sale_id,
    			"brand_id"=>$brand_id,
    			"status"=>$stt
    	);
    	return $this->update($productData, "product_id = $proID");
    }
    
    public function delProduct($proId){
    	return $this->delete("product_id = $proId");
    }
    
    public function changeStatus($proID){
    	$pro = $this->fetchRow("product_id = $proID");
    	if($pro["status"] == 0){
    		return $this->update(array("status"=>1), "product_id = $proID");
    	}else{
    		return $this->update(array("status"=>0), "product_id = $proID");
    	}
    }
}

